//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Lattice.h"
using namespace Cephei::QL;
#include <gen/QL/DiscretizedAsset.h>
#include <gen/QL/TimeGrid.h>
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::CLattice::CLattice (boost::shared_ptr<QuantLib::Lattice>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLattice = NULL;
#endif
	_ppLattice = &childNative;
    
}
Cephei::QL::CLattice::CLattice (QuantLib::Lattice& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phLattice = NULL;
#endif
	_ppLattice = new boost::shared_ptr<QuantLib::Lattice> (&childNative);
    
    _LatticeOwner = owner;
    
}

Cephei::QL::CLattice::CLattice (CLattice^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLattice = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLattice = new boost::shared_ptr<QuantLib::Lattice> (copy->GetShared());
        
    }
}
Cephei::QL::CLattice::CLattice (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLattice = NULL;
#endif
	if (!t->IsSubclassOf(CLattice::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::CLattice::CLattice (QuantLib::Handle<QuantLib::Lattice>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLattice = &childNative;
	_ppLattice = &static_cast<boost::shared_ptr<QuantLib::Lattice>>(childNative.currentLink());
    
    _LatticeOwner = owner;
}
Cephei::QL::CLattice::CLattice (QuantLib::Handle<QuantLib::Lattice> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLattice = &childNative;
	_ppLattice = &static_cast<boost::shared_ptr<QuantLib::Lattice>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::CLattice::CLattice (QuantLib::Lattice childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLattice = NULL;
#endif
	_ppLattice = new boost::shared_ptr<QuantLib::Lattice> (new QuantLib::Lattice (childNative));
    
}
#endif

Cephei::QL::CLattice::~CLattice ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppLattice != NULL)
    {
	    delete _ppLattice;
        _ppLattice = NULL;
    }
}
Cephei::QL::CLattice::!CLattice ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppLattice != NULL)
    {
	    delete _ppLattice;
    }
}
QuantLib::Lattice& Cephei::QL::CLattice::GetReference ()
{
    if (_ppLattice == NULL) throw gcnew NativeNullException ();
	return **_ppLattice;
}
boost::shared_ptr<QuantLib::Lattice>& Cephei::QL::CLattice::GetShared ()
{
    if (_ppLattice == NULL) throw gcnew NativeNullException ();
	return *_ppLattice;
}
QuantLib::Lattice* Cephei::QL::CLattice::GetPointer ()
{
    if (_ppLattice == NULL) throw gcnew NativeNullException ();
	return &**_ppLattice;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Lattice>& Cephei::QL::CLattice::GetHandle ()
{
	if (_phLattice == NULL)
	{
		_phLattice = new Handle<QuantLib::Lattice> (*_ppLattice);
	}
	return *_phLattice;
}
#endif
bool Cephei::QL::CLattice::HasNative () 
{
	return (_ppLattice != NULL);
}

Cephei::QL::ILattice^ Cephei::QL::CLattice::Initialize (Cephei::QL::IDiscretizedAsset^ prm1, Double time)
{
    CDiscretizedAsset^ _Cprm1;
    try
    {
        _Cprm1 = safe_cast<CDiscretizedAsset^> (prm1);
        _Cprm1->Lock();
        QuantLib::DiscretizedAsset& _prm1 = static_cast<QuantLib::DiscretizedAsset&> (_Cprm1->GetReference ()); 
        QuantLib::Time _time = (QuantLib::Time)ValueHelper::Convert (time);
    	(*_ppLattice)->initialize ( _prm1,  _time );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
Cephei::QL::ILattice^ Cephei::QL::CLattice::PartialRollback (Cephei::QL::IDiscretizedAsset^ prm1, Double to)
{
    CDiscretizedAsset^ _Cprm1;
    try
    {
        _Cprm1 = safe_cast<CDiscretizedAsset^> (prm1);
        _Cprm1->Lock();
        QuantLib::DiscretizedAsset& _prm1 = static_cast<QuantLib::DiscretizedAsset&> (_Cprm1->GetReference ()); 
        QuantLib::Time _to = (QuantLib::Time)ValueHelper::Convert (to);
    	(*_ppLattice)->partialRollback ( _prm1,  _to );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
Double Cephei::QL::CLattice::PresentValue (Cephei::QL::IDiscretizedAsset^ prm1)
{
    CDiscretizedAsset^ _Cprm1;
    try
    {
        _Cprm1 = safe_cast<CDiscretizedAsset^> (prm1);
        _Cprm1->Lock();
        QuantLib::DiscretizedAsset& _prm1 = static_cast<QuantLib::DiscretizedAsset&> (_Cprm1->GetReference ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLattice)->presentValue ( _prm1 );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
Cephei::QL::ILattice^ Cephei::QL::CLattice::Rollback (Cephei::QL::IDiscretizedAsset^ prm1, Double to)
{
    CDiscretizedAsset^ _Cprm1;
    try
    {
        _Cprm1 = safe_cast<CDiscretizedAsset^> (prm1);
        _Cprm1->Lock();
        QuantLib::DiscretizedAsset& _prm1 = static_cast<QuantLib::DiscretizedAsset&> (_Cprm1->GetReference ()); 
        QuantLib::Time _to = (QuantLib::Time)ValueHelper::Convert (to);
    	(*_ppLattice)->rollback ( _prm1,  _to );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
Cephei::QL::ITimeGrid^ Cephei::QL::CLattice::TimeGrid::get ()
{
    try
    {
    	QuantLib::TimeGrid& _rv = (QuantLib::TimeGrid&)(*_ppLattice)->timeGrid ( );   
        Cephei::QL::CTimeGrid^ _nrv = gcnew Cephei::QL::CTimeGrid (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

